let mobile = document.querySelector('.mobile');
let password = document.querySelector('.password');
let verifyCode = document.querySelector('.verifyCode');
let verBtn =document.querySelector('.verification');
let regBtn = document.querySelector('#reg-button');
regBtn.disabled = true;
//4项需要验证：手机号格式、密码格式、协议是否勾选
let flag1 = flag2 = flag3 = false

//失去焦点时，验证输入的手机号格式
mobile.onblur = ()=>{
    let regular =  /^1[3|4|5|7|8][0-9]{9}$/;
    if(regular.test(mobile.value)){
        flag1 = true;
    }else{
        alert('您输入的手机号格式不正确！')
        flag1 = false;
        //输入的手机号不正确时，不能发验证码请求
        verBtn.disabled = true;
    }
    isOk();
}

//失去焦点时，验证输入的密码格式
password.onblur = ()=>{
    //密码强度正则，最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
    let regular = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
    if(regular.test(password.value)){
        flag2 = true;
    }else{
        alert('您输入的密码格式不正确！');
        flag2 = false;
    }
    isOk();
}

//发送请求，获取验证码
verBtn.onclick = ()=>{
    console.log(3)
    ajax({
        url:'http://phpclub.org.cn:8080/api/user/sendVerify',
        data:`mobile=${mobile.value}`,
        success:function(response){
            if(response.code == 0){
                console.log(response);
                verifyCode.value = response.data.verifyCode;
            }
        }
    })
}

//检查协议框是否勾上
let checkbox =document.querySelector('.checkbox');
checkbox.onclick = ()=>{
    (checkbox.checked == true) ? (flag3 = true):(alert('协议框未勾选'));
    isOk();
}

function isOk(){
    (flag1 && flag2 && flag3)?(regBtn.disabled = false) : (regBtn.disabled = true);
}
// console.log(regBtn.disabled)
regBtn.onclick = ()=>{
    console.log(4);
    ajax({
        url:'http://phpclub.org.cn:8080/api/user/register',
        type:'post',
        data:{mobile:mobile.value,verifyCode:verifyCode.value,password:password.value},
        contentType:'application/json',
        success:(response)=>{
            if(response.code == 0){
                console.log(response);
                location.href = 'login.html';
            }else{
                alert(response.msg);
            }
        }
    })
};